package com.ieds.modules.common.elasticsearch;

import com.ieds.modules.common.Util.StringUtil;
import org.apache.log4j.Logger;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFTable;
import org.apache.poi.hslf.usermodel.HSLFTableCell;
import org.apache.poi.hslf.usermodel.HSLFTextShape;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTableCell;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

import java.io.*;
import java.util.List;

/**
 * 全文检索ppt转换文本
 * @author hugejile
 * @version 20200903
 */
public class PPTXFileTextExtractor {

	/**
	 * 创建log对象
	 */
	private static final Logger log = Logger.getLogger(PPTXFileTextExtractor.class);

	private final static String ppt = ".ppt";
	private final static String pptx = ".pptx";

	public static String getText(File file) {
		InputStream fileInputStream = null;

		SlideShow slideShow = null;
		//文本内容
		StringBuilder content = new StringBuilder();
		try {
			fileInputStream = new FileInputStream(file);
			if (file.getPath().endsWith(ppt)) {
				slideShow = new HSLFSlideShow(fileInputStream);
			} else if (file.getPath().endsWith(pptx)) {
				slideShow = new XMLSlideShow(fileInputStream);
			}
			if (slideShow != null) {
				//一页一页读取
				for (Slide slide : (List<Slide>)slideShow.getSlides()) {
					List shapes = slide.getShapes();
					if (shapes != null) {
						for (int i = 0; i < shapes.size(); i ++) {
							Shape shape = (Shape) shapes.get(i);
							shape.getSheet();
							//ppt的文本框
							if (shape instanceof HSLFTextShape) {
								String text = ((HSLFTextShape) shape).getText();
								content.append(text);
							}

							//pptx的文本框
							if (shape instanceof XSLFTextShape) {
								String text = ((XSLFTextShape) shape).getText();
								content.append(text);
							}

							//ppt的表格
							if (shape instanceof HSLFTable) {
								int rowSize = ((HSLFTable) shape).getNumberOfRows();
								int columnSize = ((HSLFTable) shape).getNumberOfColumns();
								for (int rowNum = 0; rowNum < rowSize; rowNum ++) {
									for (int columnNumber = 0; columnNumber < columnSize; columnNumber ++) {
										HSLFTableCell cell = ((HSLFTable) shape).getCell(rowNum, columnNumber);
										if (cell != null) {
											String text = cell.getText();
											content.append(text);
										}
									}
								}
							}

							//pptx的表格
							if (shape instanceof XSLFTable) {
								int rowSize = ((XSLFTable) shape).getNumberOfRows();
								int columnSize = ((XSLFTable) shape).getNumberOfColumns();
								for (int rowNum = 0; rowNum < rowSize; rowNum ++) {
									for (int columnNumber = 0; columnNumber < columnSize; columnNumber ++) {
										XSLFTableCell cell = ((XSLFTable) shape).getCell(rowNum, columnNumber);
										if (cell != null) {
											String text = cell.getText();
											content.append(text);
										}
									}
								}
							}
						}
					}
				}
			}
		} catch (FileNotFoundException e) {
			log.error("getText() 失败！", e);
		} catch (IOException e) {
			log.error("getText() 失败！", e);
		} finally {
			try {
				if (slideShow != null) {
					slideShow.close();
				}
				if (fileInputStream != null) {
					fileInputStream.close();
				}
			} catch (IOException e) {
				log.error("getText() 失败！", e);
			}
		}
		return StringUtil.washString(content.toString());
	}

	/*public static void main(String[] args) {
		File file = new File("E:\\ceshi\\ceshi222.pptx");
		PPTXFileTextExtractor.getText(file);
	}*/

}
